package com.gxa.ssm251.controller;

import com.gxa.ssm251.exception.BusinessException;
import com.gxa.ssm251.pojo.Admin;
import com.gxa.ssm251.pojo.City;
import com.gxa.ssm251.pojo.Order;
import com.gxa.ssm251.service.AdminService;
import com.gxa.ssm251.service.OrderService;
import com.gxa.ssm251.util.JsonResult;
import com.gxa.ssm251.util.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

//@Controller("cpController")
//@Controller
@RestController
@RequestMapping("/Order")
@Api(value = "执行订单管理的控制器", tags = "订单管理接口的api")
@CrossOrigin //跨域的注解
public class OrderController {

//    @Autowired
//    @Resource
//    @Qualifier("cpService")
//    private CpService cpService;

    @Resource
    private OrderService orderService;


    @ApiOperation(value = "查询全部订单的接口", notes = "在这里需要参数page,size",
            response = Map.class, httpMethod = "get/post")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "order", value = "json格式的订单对象", dataType = "json")
    )
    @PostMapping("/allOrder")
    @ResponseBody
    public JsonResult allOrder(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Order> orders = orderService.selectAllOrder(map.get("page").toString(), map.get("size").toString());
        Integer count=orderService.selectCount();
        if (orders.size() > 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("返回成功");
            jsonResult.setValue("count",count);
            jsonResult.setData(orders);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("返回值为空");
        }

        return jsonResult;
    }

    @ApiOperation(value = "添加订单的接口")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "order", value = "json格式的管理员对象", dataType = "json")
    )
    @PostMapping("/addOrder")
    @ResponseBody
    public JsonResult addOrder(@RequestBody Order order) {
        JsonResult jsonResult = new JsonResult();
        Integer lines=orderService.add(order);
        if (lines>0){
            jsonResult.setMsg("添加成功");
            jsonResult.setCode("1");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("未知原因,添加失败");
        }
        return jsonResult;
    }



}
